using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace IppPrinterInstallHelper
{
    internal sealed class InstallResultForm : Form
    {
        private readonly string _printerName;
        private readonly string _printerUri;

        private readonly CheckBox _setDefaultCheckBox;
        private readonly Button _testPageButton;
        private readonly Button _finishButton;

        public InstallResultForm(bool success, string printerName, string printerUri)
        {
            _printerName = printerName;
            _printerUri = printerUri;

            Text = "添加打印机向导";
            StartPosition = FormStartPosition.CenterParent;
            FormBorderStyle = FormBorderStyle.FixedDialog;
            MaximizeBox = false;
            MinimizeBox = false;
            ClientSize = new Size(640, 300);

            var titleLabel = new Label
            {
                AutoSize = true,
                Text = success
                    ? $"已成功添加 {_printerUri} 上的 {_printerName}"
                    : $"添加打印机 {_printerName} 时出现问题。",
                Location = new Point(20, 30),
                Font = new Font(SystemFonts.MessageBoxFont.FontFamily, 11f, FontStyle.Regular)
            };

            var nameLabel = new Label
            {
                AutoSize = true,
                Text = "打印机名称(P):",
                Location = new Point(20, 80)
            };

            var nameTextBox = new TextBox
            {
                Location = new Point(140, 76),
                Size = new Size(460, 22),
                ReadOnly = true,
                Text = _printerName
            };

            _setDefaultCheckBox = new CheckBox
            {
                AutoSize = true,
                Text = "设置为默认打印机(D)",
                Location = new Point(20, 120)
            };

            _testPageButton = new Button
            {
                Text = "打印测试页(&P)",
                Location = new Point(20, 160),
                Size = new Size(120, 26)
            };
            _testPageButton.Click += TestPageButton_Click;

            _finishButton = new Button
            {
                Text = "完成(&F)",
                DialogResult = DialogResult.OK,
                Location = new Point(520, 250),
                Size = new Size(80, 26)
            };
            _finishButton.Click += FinishButton_Click;

            Controls.Add(titleLabel);
            Controls.Add(nameLabel);
            Controls.Add(nameTextBox);
            Controls.Add(_setDefaultCheckBox);
            Controls.Add(_testPageButton);
            Controls.Add(_finishButton);

            AcceptButton = _finishButton;
        }

        private void FinishButton_Click(object sender, EventArgs e)
        {
            if (_setDefaultCheckBox.Checked)
            {
                try
                {
                    var psi = new ProcessStartInfo
                    {
                        FileName = "rundll32.exe",
                        Arguments = $"printui.dll,PrintUIEntry /y /n \"{_printerName}\"",
                        UseShellExecute = true
                    };
                    Process.Start(psi);
                }
                catch
                {
                }
            }
        }

        private void TestPageButton_Click(object sender, EventArgs e)
        {
            try
            {
                var psi = new ProcessStartInfo
                {
                    FileName = "rundll32.exe",
                    Arguments = $"printui.dll,PrintUIEntry /k /n \"{_printerName}\"",
                    UseShellExecute = true
                };
                Process.Start(psi);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this,
                    "无法打印测试页：\n" + ex.Message,
                    "错误",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }
    }
}


